// Copyright (c) 2024, json_wang and contributors
// For license information, please see license.txt

// frappe.ui.form.on("T001", {
// 	refresh(frm) {

// 	},
// });
frappe.ui.form.on('T001', {
    refresh: function(frm) {
        frm.add_custom_button(__('下载XML文件'), function() {
            console.log(frm);
            window.open('/api/method/xingxiang.%E5%85%B4%E6%B9%98%E6%95%B0%E6%8D%AE%E5%A1%AB%E6%8A%A5.xml_service.get_doc_asxml?doc_name='+frm.doc.name,'_blank')
        });
    }
});

frappe.ui.form.on('T001-1', { // The child table is defined in a DoctType called "Dynamic Link"
    ljwcs(frm, cdt, cdn) { // "links" is the name of the table field in ToDo, "_add" is the event
        var child = locals[cdt][cdn];
        calculateC(child,cdt,cdn);
    },
	bnmbs(frm, cdt, cdn){
		var child = locals[cdt][cdn];
        calculateC(child,cdt,cdn);
	}
});

function calculateC(child,cdt,cdn) {
    var ljwcs = child.ljwcs;
    var bnmbs = child.bnmbs;
    if (ljwcs && bnmbs && bnmbs > 0) {
        var ljwcl = ljwcs / bnmbs * 100;
        frappe.model.set_value(child.doctype, child.name, 'ljwcl', ljwcl);
        return
    }
    frappe.model.set_value(cdt, cdn, 'ljwcl', 0);
}